package com.lm.activiti.boot;

import com.google.common.collect.Lists;
import com.lm.activiti.boot.dto.ExifUnionAttenSwipingDto;
import com.lm.activiti.boot.template.BaseRestTestTemplate;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;

/**
 * @author lm_jcf
 * @date 2020/4/1 15:41
 */
public class AttenSchoolClockTest extends BaseRestTestTemplate {

    String url = "http://cs.52ess.cn/ems/nzp-ems-exif-server/rest/union/atten/swiping";

    private List<ExifUnionAttenSwipingDto> list = Lists.newArrayList();

    @BeforeClass
    public void buildSwipingData() throws IOException {
        Workbook workbook = new XSSFWorkbook(new FileInputStream("C:\\Users\\lm_jcf\\Desktop\\加州小学卡号.xlsx"));

        Sheet sheet = workbook.getSheetAt(0);

        int lastRowNum = sheet.getLastRowNum();
        for (int i = 0; i < lastRowNum; i++) {
            Row row = sheet.getRow(i);

            if (Objects.isNull(row)) {
                continue;
            }
            Cell cell = row.getCell(0);
            if (Objects.nonNull(cell) && StringUtils.isNotBlank(cell.getStringCellValue())) {
                ExifUnionAttenSwipingDto dto = new ExifUnionAttenSwipingDto();
                dto.setCardNo(cell.getStringCellValue());
                dto.setDeviceId("44010601016431");
                dto.setAttenTime("20200402152100");
                dto.setAttenType("1");
                dto.setToken("0ps5o7FqODQZbJqxPpNqoUktv0HT7v6DGdHH8lsdI46eCR8D");

                list.add(dto);
            }
        }
        workbook.close();
    }

    volatile int indexTally = 0;

    @Test
    public void singleSwiping() {

        ExifUnionAttenSwipingDto exifUnionAttenSwipingDto = list.get(indexTally);

        httpPost(url, exifUnionAttenSwipingDto);

        indexTally++;
    }

    @Test
    public void swiping() throws IOException, InterruptedException {

//        this.buildSwipingData();

        final int count = list.size();

        final CountDownLatch downLatch = new CountDownLatch(count); // 并发数

        for (int i = 0; i < count; i++) {
            final ExifUnionAttenSwipingDto dto = list.get(i);

            final int j = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        downLatch.countDown();

                        downLatch.await();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

//                    httpPost(url, dto);

                    System.out.println("resp:{}" + j);
                    indexTally++;
                }
            }).start();
        }
        downLatch.await();

        Thread.currentThread().join();

        System.out.println("总数：" + indexTally);
    }

    @Test
    public void testBeforeClass(){
        System.out.println("已经初始化了：" + list.size());
    }
}
